﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wp.DAL;
using Wp.BLL;
using System.Web.UI.HtmlControls;

public partial class Modules_Posts_Controls_ListPost : System.Web.UI.UserControl
{
    public static int PAGE_SIZE = 20;
    public string CategoryName { get; set; }
    public string CategoryTextId { get; set; }
    public int PageIndex { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        PageIndex = CommonHelper.QueryStringInt("PageIndex", 1);
        CategoryTextId = CommonHelper.QueryString("CategoryTextId", "");
        if (!IsPostBack)
        {
            LoadData();
        }
    }
    void LoadData()
    {
        TblTermsEntity item = TblTermsManager.CreateInstant().SelectOneSlug(CategoryTextId);
        if (item != null)
            CategoryName = item.Name;
        else CategoryName = "";
        int TotalRow = 0;
        List<TblPostsEntity> ls = TblPostsManager.CreateInstant().SelectTblPostBy("post", "", -1, CategoryTextId, "category", "CreatedDate", "DESC", PAGE_SIZE, PageIndex, out TotalRow);
        rpt.DataSource = ls;
        rpt.DataBind();
        smartPager1.PageSize = PAGE_SIZE;
        smartPager1.QueryStringKey = "PageIndex";
        smartPager1.RowCount = TotalRow;
        smartPager1.NavigatePreviousText = "<<";
        smartPager1.NavigateNextText = ">>";
        //smartPager1.Display = 7;
        smartPager1.UrlFormatString = UrlBuilder.GetCategoryPaging(item.Slug);
        smartPager1.Visible = TotalRow > PAGE_SIZE;
        SEO(CategoryName, ls);
    }
    void SEO(string TaxonomyName, List<TblPostsEntity> ls)
    {
        string imageurl = "";
        if (ls != null && ls.Count > 0)
        {
            imageurl = ls[0].ImageUrl;
        }
        string Title = TaxonomyName + " - " + TblOptionManager.CreateInstant().GetOption("SEOTitle");
        string description = TblOptionManager.CreateInstant().GetOption("SEODescription");
        string keywords = TblOptionManager.CreateInstant().GetOption("SEOKeyWord");
        string url = Wp.BLL.TblOptionManager.CreateInstant().GetDomainName() + Request.RawUrl;
        imageurl = UrlBuilder.GetImage(imageurl, 100);
        this.Page.Title = Title;
        SetHeaderMetName("description", description);
        SetHeaderMetName("keywords", keywords);

        SetHeaderMetProperty("og:title", Title);
        SetHeaderMetProperty("og:type", "article");
        SetHeaderMetProperty("og:url", url);
        SetHeaderMetProperty("og:image", imageurl);
        SetHeaderMetProperty("og:description", description);
        SetHeaderMetProperty("og:site_name", "Linh Sơn Phật Giáo");
    }
    private void SetHeaderMetName(string Name, string Content)
    {
        HtmlMeta meta = new HtmlMeta();
        meta.Name = Name;
        meta.Content = Content;
        this.Page.Master.FindControl("head").Controls.AddAt(this.Page.Master.FindControl("head").Controls.Count - 1, meta);
    }
    private void SetHeaderMetProperty(string Name, string Content)
    {
        HtmlMeta meta = new HtmlMeta();
        meta.Attributes.Add("property", Name);
        meta.Content = Content;
        this.Page.Master.FindControl("head").Controls.AddAt(this.Page.Master.FindControl("head").Controls.Count - 1, meta);
    }
    public string GetCodeEdit(object id)
    {
        if (HttpContext.Current.User.Identity.IsAuthenticated)
        {
            return "<a href='/admin/Modules/Posts/PostEdit.aspx?Id=" + id + "'  target='_blank'>Edit</a>";
        }
        return "";
    }
    public string GetAvaliableDelete()
    {
        string result = "style='display:none'";
        if (HttpContext.Current.User.Identity.IsAuthenticated)
        {
            result = "style='display:inline'";
        }
        
        return result;
    }
}